package com.zjmzxfzhl.modules.app.controller;

import java.util.*;

import javax.validation.Valid;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.modules.app.entity.AppTerm;
import com.zjmzxfzhl.modules.app.form.AppTermUserVo;
import com.zjmzxfzhl.modules.app.form.AppTermVo;
import com.zjmzxfzhl.modules.app.service.AppTermService;
import com.zjmzxfzhl.modules.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.common.core.base.BaseController;
import com.zjmzxfzhl.modules.app.entity.AppTermUser;
import com.zjmzxfzhl.modules.app.service.AppTermUserService;

/**
 * Controller
 * 
 * @author 张森荣
 */
@RestController
@RequestMapping("/app/termUser")
public class AppTermUserController extends BaseController {
    @Autowired
    private AppTermUserService appTermUserService;

    @Autowired
    private AppTermService appTermService;
    /**
     * 自定义查询列表
     * 
     * @param appTermUserVo
     * @return
     */
//    @PreAuthorize("@elp.single('app:termUser:list')")
    @GetMapping(value = "/list")
    public Result list(AppTermUserVo appTermUserVo,@RequestParam Integer current, @RequestParam Integer size) {
        appTermUserVo.setStatus("1");
        IPage<AppTermUserVo> pageList = appTermUserService.list(new Page<AppTermUserVo>(current, size),appTermUserVo);
        return Result.ok(pageList);
    }

    @PreAuthorize("@elp.single('app:termUser:list')")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        AppTermUser appTermUser = appTermUserService.getById(id);
        return Result.ok(appTermUser);
    }

    /**
     * @功能：新增
     * @param appTermUser
     * @return
     */
//    @PreAuthorize("@elp.single('app:termUser:save')")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody AppTermUser appTermUser) {
        Boolean result=appTermUserService.saveAppProj(appTermUser);
        if(result==false){
            return Result.error("检查成员是否已在团队！");
        }
        AppTermUserVo appTermUserVo=new AppTermUserVo();
        appTermUserVo.setTermId(appTermUser.getTermId());
        appTermUserVo.setUserId(appTermUser.getUserId());
        List<AppTermUserVo> pageList = appTermUserService.getList(null,appTermUserVo);
        return Result.ok(pageList);
    }

    /**
     * @功能：修改
     * @param appTermUser
     * @return
     */
    @PreAuthorize("@elp.single('app:termUser:update')")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody AppTermUser appTermUser) {
        appTermUserService.updateById(appTermUser);
        return Result.ok();
    }

    /**
     * @功能：批量删除
     * @param ids
     * @return
     */
    @PreAuthorize("@elp.single('app:termUser:delete')")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        String[] idsArr = ids.split(",");
        if (idsArr.length > 1) {
            appTermUserService.removeByIds(Arrays.asList(idsArr));
        } else {
            appTermUserService.removeById(idsArr[0]);
        }
        return Result.ok();
    }

    /**
     * @功能：删除关联
     * @param appTermUser
     * @return
     */
//    @PreAuthorize("@elp.single('app:projGit:delete')")
    @DeleteMapping(value = "/relationDel")
    public Result relationDel(AppTermUser appTermUser) {
        if(appTermUser.getUserId()==null){
            return Result.error("成员编号不能为空！");
        }
        if(appTermUser.getTermId()==null){
            return Result.error("团队编号不能为空！");
        }
        Boolean result=appTermUserService.relationDel(appTermUser);
        if(result==false){
            return Result.error("删除关联失败！");
        }
        return Result.ok();
    }

    /**
     * 查询团队成员
     *
     * @return
     */
//    @PreAuthorize("@elp.single('app:term:list')")
    @GetMapping(value = "/getTreeData")
    public Result termUser(AppTermUserVo appTermUserVo) {
        List<Map<String,Object>> mapList=new ArrayList<>();
        AppTermVo appTermVo=new AppTermVo();
        appTermVo.setTermId(appTermUserVo.getTermId());
        appTermVo.setUserName(appTermUserVo.getUserName());
        List<AppTerm> appTerms= appTermService.getList(null, appTermVo);
        for (AppTerm term:appTerms) {
            Map<String,Object> map=new HashMap<>();
            appTermUserVo.setTermId(term.getTermId());
            appTermUserVo.setStatus("1");
            List<SysUser> userList=appTermUserService.getTermUserList(appTermUserVo);
            map.put("termName",term.getName());
            map.put("userList",userList);
            mapList.add(map);
        }
        //没有团队的成员
        List<SysUser> otherUserList = appTermUserService.getOtherUserList(appTermUserVo);
        if (otherUserList.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("termName", "其他成员");
            map.put("userList", otherUserList);
            mapList.add(map);
        }
        return Result.ok(mapList);
    }

    /**
     * 批量添加成员
     * @param termId
     * @param userIds
     * @return
     */
//    @PreAuthorize("@elp.single('app:projGit:delete')")
    @PostMapping(value = "/batchAdd")
    public Result batchAdd(String termId,String userIds) {

        if(termId == null || termId.trim().length() == 0){
            return Result.error("团队编号不能为空！");
        }
        if(userIds == null || userIds.trim().length() == 0){
            return Result.error("成员编号不能为空！");
        }
        Boolean result=appTermUserService.batchAdd(termId,userIds);
        if(result==false){
            return Result.error("添加成员失败失败！");
        }
        return Result.ok();
    }
}
